/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.charts.EdgeDetail;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import org.jetbrains.annotations.NonNls;

public class SampleFit2Ability extends Ability {
    private int mSeries1Index;
    private String mProgress;

    final private int COLOR_BLUE = RgbPalette.parse("#AA1D76D2");
    final private int COLOR_PINK = RgbPalette.parse("#AAFF4081");
    final private int COLOR_YELLOW = RgbPalette.parse("#AAFFC107");
    final private int COLOR_GREEN = RgbPalette.parse("#AA07CC07");
    final private int COLOR_EDGE = RgbPalette.parse("#22000000");
    final private int COLOR_BACK = RgbPalette.parse("#22888888");
    final private int COLOR_NEUTRAL = RgbPalette.parse("#FF999999");

    private DecoView decoView;
    private Text textProgress;
    private Image imageViewAvatar;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_fit_2);

        decoView = (DecoView) findComponentById(ResourceTable.Id_decoView);
        textProgress = (Text) findComponentById(ResourceTable.Id_textProgress);
        imageViewAvatar = (Image) findComponentById(ResourceTable.Id_imageViewAvatar);

        createTracks();
        setupEvents();
    }

    private void createTracks() {
        decoView.executeReset();
        decoView.deleteAll();

        final float mSeriesMax = 100f;

        SeriesItem seriesBack1Item = new SeriesItem.Builder(COLOR_BACK)
                .setRange(0, mSeriesMax, mSeriesMax)
                .setLineWidth(Utility.getDimension(46, getApplicationContext()))
                .build();

        decoView.addSeries(seriesBack1Item);

        SeriesItem series1Item = new SeriesItem.Builder(COLOR_NEUTRAL)
                .setRange(0, mSeriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(46, getApplicationContext()))
                .setCapRounded(true)
                .addEdgeDetail(new EdgeDetail(EdgeDetail.EdgeType.EDGE_INNER, COLOR_EDGE, 0.3f))
                .setShowPointWhenEmpty(true)
                .build();

        mSeries1Index = decoView.addSeries(series1Item);

        addFitListener(series1Item, textProgress);
    }

    private void setupEvents() {
        addAnimation(decoView, mSeries1Index, 79.7f, 2000, imageViewAvatar, ResourceTable.Media_ic_fit_bike, "Cycle %.0f Km", COLOR_GREEN, false);
        addAnimation(decoView, mSeries1Index, 16.4f, 9000, imageViewAvatar, ResourceTable.Media_ic_fit_run, "Run %.1f Km", COLOR_YELLOW, false);
        addAnimation(decoView, mSeries1Index, 58f, 16000, imageViewAvatar, ResourceTable.Media_ic_fit_gym, "Gym %.0f min", COLOR_PINK, false);
        addAnimation(decoView, mSeries1Index, 3.38f, 23000, imageViewAvatar, ResourceTable.Media_ic_fit_swim, "Swim %.2f Km", COLOR_BLUE, true);
    }

    private void addAnimation(final DecoView arcView,
                              int series, float moveTo, int delay,
                              final Image imageView, final int imageId,
                              final String format, final int color, final boolean restart) {

        DecoEvent.ExecuteEventListener listener = new DecoEvent.ExecuteEventListener() {
            @Override
            public void onEventStart(DecoEvent event) {
                imageView.setBackground(Utility.getDrawable(getApplicationContext(), imageId));
                mProgress = format;
            }

            @Override
            public void onEventEnd(DecoEvent event) {
                imageView.setBackground(null);
                if (restart) {
                    setupEvents();
                }
            }
        };

        arcView.addEvent(new DecoEvent.Builder(moveTo)
                .setIndex(series)
                .setDelay(delay)
                .setDuration(5000)
                .setListener(listener)
                .setColor(color)
                .build());
    }

    private void addFitListener(@NonNls final SeriesItem seriesItem, @NonNls final Text view) {
        seriesItem.addArcSeriesItemListener(new SeriesItem.SeriesItemListener() {

            @Override
            public void onSeriesItemAnimationProgress(float percentComplete, float currentPosition) {
                if (mProgress != null) {
                    if (mProgress.contains("%%")) {
                        view.setText(String.format(mProgress, (1.0f - (currentPosition / seriesItem.getMaxValue())) * 100f));
                    } else {
                        view.setText(String.format(mProgress, currentPosition));
                    }
                }
            }

            @Override
            public void onSeriesItemDisplayProgress(float percentComplete) {

            }
        });
    }
}